package com.tsxy.mall.mapper;

import com.tsxy.mall.entity.Book;
import com.tsxy.mall.entity.Cart;
import org.apache.ibatis.annotations.*;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CartMapper {

    @Insert({"insert into cart (user_id , book_id, count,book_type) values ( #{userId} , #{bookId},#{count},#{bookType} )"})
    Integer insert(Cart cart);

    @Select({"select count from cart where user_id = #{userId} and book_id = #{bookId} "})
    Integer queryByBookUser(Cart cart);

    @Update({"update cart set count = count + 1 where user_id = #{userId} and book_id = #{bookId} "})
    Integer update(Cart cart);

    @Select({"select * from cart"})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "book_id",property = "bookId"),
            @Result(column = "count",property = "count"),
            @Result(column = "book_id",property = "book",
                one = @One(select = "com.tsxy.mall.mapper.BookMapper.selectById"))
    })
    List<Cart> getAll();

    @Select({"select * from cart where user_id = #{id} and book_type = 0 "})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "book_id",property = "bookId"),
            @Result(column = "count",property = "count"),
            @Result(column = "book_type",property = "bookType"),
            @Result(column = "book_id",property = "book",
                    one = @One(select = "com.tsxy.mall.mapper.BookMapper.selectById"))
    })
    List<Cart> getAllBook(@Param("id") Integer id);

    @Select({"select * from cart where user_id = #{id} and book_type = 1 "})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "book_id",property = "bookId"),
            @Result(column = "count",property = "count"),
            @Result(column = "book_type",property = "bookType"),
            @Result(column = "book_id",property = "usedBook",
                    one = @One(select = "com.tsxy.mall.mapper.UsedBookMapper.getById"))
    })
    List<Cart> getUsedBook(Integer id);

    @Update({
            "update cart set count = #{count} where id = #{id}"
            })
    Integer updateCount(@Param("id") Integer id, @Param("count") Integer count);

    @Select({"select * from cart where id = #{id} "})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "book_id",property = "bookId"),
            @Result(column = "count",property = "count"),
            @Result(column = "book_type",property = "bookType"),
            @Result(column = "book_id",property = "book",
                    one = @One(select = "com.tsxy.mall.mapper.BookMapper.selectById")),
            @Result(column = "book_id",property = "usedBook",
                    one = @One(select = "com.tsxy.mall.mapper.UsedBookMapper.getById"))
    })
    Cart getById(Integer id);

    @Delete({"delete from cart where id = #{id}"})
    int deleteByPrimaryKey(@Param("id") Integer id);
}
